<template>
  <div class="app-container">
    <div class="filter-container">
      <el-select v-model="downloadParam.supplierChannelIds" placeholder="供应商" clearable multiple filterable style="width: 140px;" class="filter-item">
        <el-option v-for="item in supplierChannelOptions" :key="item.key" :label="item.display_name" :value="item.key" />
      </el-select>
      <el-button class="filter-item" type="primary" style="margin-left: 10px;" @click="handleDownload">
        下载
      </el-button>
    </div>
  </div>
</template>

<script>
import { Message } from 'element-ui'
import { fetchEnum } from '@/api/system-api'
import { downloadInventory } from '@/api/manager/goods-api'
import { toOptionsList, handleDownloadWithUtf8Filename } from '@/utils/index'

export default {
  name: 'ManagerInventoryDownload',
  filters: {
  },
  data() {
    return {
      supplierChannelOptions: [],
      downloadParam: {
        supplierChannelIds: []
      },
      exportLoading: false
    }
  },
  async created() {
    await this.initLoad()
  },
  methods: {
    async initLoad() {
      const enumCodes = ['SUPPLIER_CHANNEL']
      const { data } = await fetchEnum(enumCodes)
      for (const e of data) {
        if (e.code === 'SUPPLIER_CHANNEL') {
          if (e.options) {
            this.supplierChannelOptions = toOptionsList(e.options, 'id', 'shortName')
          }
        }
      }
    },

    handleDownload() {
      if (this.downloadParam && this.downloadParam.supplierChannelIds && this.downloadParam.supplierChannelIds.length >= 1) {
        const param = {
          'supplierChannelIdList': this.downloadParam.supplierChannelIds
        }
        this.exportLoading = true
        downloadInventory(param).then(response => {
          handleDownloadWithUtf8Filename(response)
          this.exportLoading = false
        }).catch(error => {
          Message({
            message: error,
            type: 'error',
            duration: 5 * 1000
          })
          this.exportLoading = false
        })
      } else {
        Message({
          message: '下载库存之前请选择供应商',
          type: 'error',
          duration: 5 * 1000
        })
      }
    }

  }
}

</script>
